<?php
namespace app\api\controller;

use think\Request;

class User extends BaseController
{
    public function login()
    {
        $data = [];
        $data['status'] = 404;

        if (Request::instance()->isPost()) {
            $user_account = input('post.user_account');
            $user_password = input('post.user_password');

            $user_model = model('User');

            $user_where = [
                'user_status' => '1',
                'is_deleted' => '0'
            ];

            $user = $user_model->where(function ($query) use ( $user_account ) {
                $query->where('user_account', $user_account)->whereor('user_mobile', $user_account);
            })->where($user_where)->find();

            if ($user && $user->user_password === md5($user_password.$user->user_salt)) {
                $data['status'] = 200;
                $data['user'] = [
                    'user_id' => $user->user_id,
                    'user_token' => $user->user_token
                ];
            }
        }

        return json($this->dataReturn($data));
    }

    public function isLogin()
    {
        $data = [];
        $data['status'] = 200;

        if (Request::instance()->isPost()) {
            $user_token = input('post.user_token');

            $user_model = model('User');

            $user_field = [
                'user_id'
            ];

            $user_where = [
                'user_token' => $user_token,
                'user_status' => '1',
                'is_deleted' => '0'
            ];

            $data['user'] = $user_model->field($user_field)->where($user_where)->find();

            $data['status'] = $data['user'] ? 200 : 404;
        }

        return json($this->dataReturn($data));
    }

    public function register()
    {
        $data = [];
        $data['error'] = [];

        if (Request::instance()->isPost()) {
            $user_account = input('post.user_account');
            $user_mobile = input('post.user_mobile');
            $user_password = input('post.user_password');

            $user_model = model('User');

            $exist_user_account = $user_model->get([
                'user_account' => $user_account
            ]);

            if ($exist_user_account) {
                $data['error']['message'] = '该账号已存在';
                $data['status'] = 401;
            }

            $exist_user_mobile = $user_model->get([
                'user_mobile' => $user_mobile
            ]);

            if ($exist_user_mobile) {
                $data['error']['message'] = '该手机号码已存在';
                $data['status'] = 401;
            }

            if (!$data['error']) {
                vendor('RandChar');

                $user_salt = \RandChar::generateRandChar();
                $user_password = md5($user_password. $user_salt);
                $user_last_login_time = time();
                $user_token = md5(time());

                $user_model->data([
                    'user_account' => $user_account,
                    'user_mobile' => $user_mobile,
                    'user_password' => $user_password,
                    'user_salt' => $user_salt,
                    'user_token' => $user_token,
                    'user_last_login_time' => $user_last_login_time
                ]);
                $user_model->save();

                $data['user']['user_id'] = $user_model->user_id;
                $data['status'] = 200;
            }
        }

        return json($this->dataReturn($data));
    }

    public function forgotPassword()
    {
        $data = [];
        $data['error'] = [];

        if (Request::instance()->isPost()) {
            $reset_valid_code = 'nXp7';
            $valid_code = input('post.valid_code');
            $user_mobile = input('post.user_mobile');
            $user_password = input('post.user_password');

            if ($reset_valid_code !== $valid_code) {
                $data['error']['message'] = '验证码错误';
                $data['status'] = 401;
            }

            $user_model = model('User');

            $exist_user = $user_model->get([
                'user_mobile' => $user_mobile
            ]);

            if (!$exist_user) {
                $data['error']['message'] = '该手机号码不存在';
                $data['status'] = 401;
            }

            if (!$data['error']) {
                vendor('RandChar');

                $user_salt = \RandChar::generateRandChar();
                $user_password = md5($user_password. $user_salt);
                $user_token = md5(time());

                $user_model->save([
                    'user_password' => $user_password,
                    'user_salt' => $user_salt,
                    'user_token' => $user_token,
                ], ['user_mobile' => $user_mobile]);
                $data['status'] = 200;
            }

        }

        return json($this->dataReturn($data));
    }
}